var csInterface = new CSInterface();

// 个人主页
var homePageElement = document.querySelector("#homePage");
homePageElement.addEventListener("click", callHomepage);

function callHomepage() {
  window.cep.util.openURLInDefaultBrowser('https://gitee.com/brzjomo');
}

// 交互按钮
var runButtonLeft = document.querySelector("#btn-left");
var runButtonRight = document.querySelector("#btn-right");
var runButtonUp = document.querySelector("#btn-up");
var runButtonDown = document.querySelector("#btn-down");
var runButtonCenter = document.querySelector("#btn-center");
runButtonLeft.addEventListener("click", buttonLeft);
runButtonRight.addEventListener("click", buttonRight);
runButtonUp.addEventListener("click", buttonUp);
runButtonDown.addEventListener("click", buttonDown);
runButtonCenter.addEventListener("click", buttonCenter);

var valueElement = document.getElementsByName("text-input")[0];
addFocusSelectEvent(valueElement);

function buttonLeft() {
  // 获取制图比例
  var drawingScaleElement = document.getElementById("drawing-scale");
  var drawingScale = parseInt(drawingScaleElement.options[drawingScaleElement.selectedIndex].value);

  // 获取布尔-是否移动时复制
  var CopyAndMoveElement = document.getElementById("ifCopyAndMove");
  var ifCopyAndMove = CopyAndMoveElement.checked;

  // 获取布尔-是否自动递增数字名称
  var ifAutoIncrementElement = document.getElementById("ifAutoIncrement");
  var ifAutoIncrement = ifAutoIncrementElement.checked;

  var direction = 4;
  var value = parseFloat(valueElement.value);

  csInterface.evalScript('lengthen(' + drawingScale + ', ' + direction + ', ' + value + ', ' + ifCopyAndMove + ', ' + ifAutoIncrement +')');
}

function buttonRight() {
  // 获取制图比例
  var drawingScaleElement = document.getElementById("drawing-scale");
  var drawingScale = parseInt(drawingScaleElement.options[drawingScaleElement.selectedIndex].value);

  // 获取布尔-是否移动时复制
  var CopyAndMoveElement = document.getElementById("ifCopyAndMove");
  var ifCopyAndMove = CopyAndMoveElement.checked;

  // 获取布尔-是否自动递增数字名称
  var ifAutoIncrementElement = document.getElementById("ifAutoIncrement");
  var ifAutoIncrement = ifAutoIncrementElement.checked;

  var direction = 6;
  var value = parseFloat(valueElement.value);

  csInterface.evalScript('lengthen(' + drawingScale + ', ' + direction + ', ' + value + ', ' + ifCopyAndMove + ', ' + ifAutoIncrement +')');
}

function buttonUp() {
  // 获取制图比例
  var drawingScaleElement = document.getElementById("drawing-scale");
  var drawingScale = parseInt(drawingScaleElement.options[drawingScaleElement.selectedIndex].value);

  // 获取布尔-是否移动时复制
  var CopyAndMoveElement = document.getElementById("ifCopyAndMove");
  var ifCopyAndMove = CopyAndMoveElement.checked;

  // 获取布尔-是否自动递增数字名称
  var ifAutoIncrementElement = document.getElementById("ifAutoIncrement");
  var ifAutoIncrement = ifAutoIncrementElement.checked;

  var direction = 8;
  var value = parseFloat(valueElement.value);

  csInterface.evalScript('lengthen(' + drawingScale + ', ' + direction + ', ' + value + ', ' + ifCopyAndMove + ', ' + ifAutoIncrement +')');
}

function buttonDown() {
  // 获取制图比例
  var drawingScaleElement = document.getElementById("drawing-scale");
  var drawingScale = parseInt(drawingScaleElement.options[drawingScaleElement.selectedIndex].value);

  // 获取布尔-是否移动时复制
  var CopyAndMoveElement = document.getElementById("ifCopyAndMove");
  var ifCopyAndMove = CopyAndMoveElement.checked;

  // 获取布尔-是否自动递增数字名称
  var ifAutoIncrementElement = document.getElementById("ifAutoIncrement");
  var ifAutoIncrement = ifAutoIncrementElement.checked;

  var direction = 2;
  var value = parseFloat(valueElement.value);

  csInterface.evalScript('lengthen(' + drawingScale + ', ' + direction + ', ' + value + ', ' + ifCopyAndMove + ', ' + ifAutoIncrement +')');
}

function buttonCenter() {
  // 获取制图比例
  var drawingScaleElement = document.getElementById("drawing-scale");
  var drawingScale = parseInt(drawingScaleElement.options[drawingScaleElement.selectedIndex].value);

  // 获取布尔-是否移动时复制
  var CopyAndMoveElement = document.getElementById("ifCopyAndMove");
  var ifCopyAndMove = CopyAndMoveElement.checked;

  // 获取布尔-是否自动递增数字名称
  var ifAutoIncrementElement = document.getElementById("ifAutoIncrement");
  var ifAutoIncrement = ifAutoIncrementElement.checked;

  var direction = 5;
  var value = parseFloat(valueElement.value);

  csInterface.evalScript('lengthen(' + drawingScale + ', ' + direction + ', ' + value + ', ' + ifCopyAndMove + ', ' + ifAutoIncrement +')');
}

function addFocusSelectEvent(element) {
  if (element) {
      element.addEventListener('focus', function() {
          this.select();
      });
  }
}
